package com.kungfuhacking.wristbandpro.login.presenter;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.login.bean.DataEntity;
import com.kungfuhacking.wristbandpro.login.bean.LoginBean;
import com.kungfuhacking.wristbandpro.login.view.ISetPwdView;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.CommonUtils;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 设置密码
 * Created by Aaron on 17/3/29.
 */

public class ISetPwdPresenter implements BasePresenter {

    private ISetPwdView iSetPwdView;

    public ISetPwdPresenter(ISetPwdView iSetPwdView) {
        this.iSetPwdView = iSetPwdView;
    }

    public void setPwd(String token) {

        String password = iSetPwdView.getPassword();
        String passwordAgain = iSetPwdView.getPasswordAgain();

        if (TextUtils.isEmpty(password)) {
            iSetPwdView.showErr("请输入密码");
            return;
        }

        if (password.length() < 6 || password.length() > 16) {
            iSetPwdView.showErr("密码在6~16位之间");
            return;
        }

        if (!password.equals(passwordAgain)) {
            iSetPwdView.showErr("两次输入密码不一致");
            return;
        }

        connectHttp(token, password);

    }

    private void connectHttp(String token, String password) {

        JSONObject j = new JSONObject();

        j.put("token", token);
        j.put("pwd", CommonUtils.toMd5(password.getBytes()));

        RetrofitHttpUtil.getInstance().register(new Observer<LoginBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iSetPwdView.showLoading("设置中...");
            }

            @Override
            public void onNext(LoginBean loginBean) {
                iSetPwdView.hideLoading();

                if (loginBean.getCode() == 0) {
                    iSetPwdView.showErr("注册成功");
                    saveUserInfo(loginBean);
                } else
                    iSetPwdView.showErr(loginBean.getCode_msg());
            }

            @Override
            public void onError(Throwable e) {
                iSetPwdView.hideLoading();
                iSetPwdView.showErr("网络错误 : " + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        }, new RequestBean().setData(j.toString()));

    }

    /**
     * 设置用户信息
     */
    private void saveUserInfo(LoginBean loginBean) {

        SharedPreferences sp = iSetPwdView.getSp();
        SharedPreferences.Editor edit = sp.edit();
        DataEntity responseBody = loginBean.getData();

        //用户信息
        edit.putString(Constant.USER_ID, responseBody.getId());
        edit.putString(Constant.MOBILE, responseBody.getMobile());
        edit.putString(Constant.NICKNAME, responseBody.getNickName());
        edit.putString(Constant.USER_IMG, responseBody.getUserImg());
        edit.putString(Constant.SEX, responseBody.getSex());
        edit.putString(Constant.BIRTHDAY, responseBody.getBirthDay());
        edit.putString(Constant.TOKEN, loginBean.getToken().getToken());
        //账号密码
        edit.putString(Constant.MOBILE, iSetPwdView.getAccount());
        edit.putString(Constant.PASSWORD, CommonUtils.toMd5(iSetPwdView.getPassword().getBytes()));
        edit.apply();

        //跳到创建机构页面
        iSetPwdView.nextActivityWithSingleTask(LoginActivity.class);

    }

    @Override
    public void clearRequest() {
//        Log.d(TAG, " ~~~ 取消网络请求 ~~~ ");
        compositeDisposable.clear();

    }
}
